const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const util = require('util')
const execAsync = util.promisify(exec)

// 图标配置
const icons = [
  {
    name: 'countdown',
    svg: `<svg width="48" height="48" viewBox="0 0 48 48" fill="none">
      <path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
      <path d="M24 12V24L32 28" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
    </svg>`
  },
  {
    name: 'income',
    svg: `<svg width="48" height="48" viewBox="0 0 48 48" fill="none">
      <path d="M24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4Z" stroke="currentColor" stroke-width="2"/>
      <path d="M16 24H32" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
      <path d="M24 16L24 32" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
    </svg>`
  },
  {
    name: 'free',
    svg: `<svg width="48" height="48" viewBox="0 0 48 48" fill="none">
      <path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" stroke="currentColor" stroke-width="2"/>
      <path d="M31 18C31 18 29 16 24 16C19 16 17 18 17 20C17 22 19 23 24 24C29 25 31 26 31 28C31 30 29 32 24 32C19 32 17 30 17 30" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
      <path d="M24 12V16" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
      <path d="M24 32V36" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
    </svg>`
  },
  {
    name: 'timer',
    svg: `<svg width="48" height="48" viewBox="0 0 48 48" fill="none">
      <path d="M24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z" stroke="currentColor" stroke-width="2"/>
      <path d="M24 12V24L32 32" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
    </svg>`
  },
  {
    name: 'settings',
    svg: `<svg width="48" height="48" viewBox="0 0 48 48" fill="none">
      <path d="M24 4L28.2 7.4C29.1 8.1 30.3 8.2 31.3 7.6L36.2 5C37.9 3.9 40.1 4.9 40.7 6.8L42.3 11.9C42.7 13 43.7 13.8 44.8 13.8H50C51.9 13.8 53.4 15.3 53.4 17.2V22.4C53.4 23.5 54.2 24.5 55.3 24.9L60.4 26.5C62.3 27.1 63.3 29.3 62.2 31L59.6 35.9C59 36.9 59.1 38.1 59.8 39L63.2 43.2C64.3 44.6 64.3 46.6 63.2 48L59.8 52.2C59.1 53.1 59 54.3 59.6 55.3L62.2 60.2C63.3 61.9 62.3 64.1 60.4 64.7L55.3 66.3C54.2 66.7 53.4 67.7 53.4 68.8V74C53.4 75.9 51.9 77.4 50 77.4H44.8C43.7 77.4 42.7 78.2 42.3 79.3L40.7 84.4C40.1 86.3 37.9 87.3 36.2 86.2L31.3 83.6C30.3 83 29.1 83.1 28.2 83.8L24 87.2C22.6 88.3 20.6 88.3 19.2 87.2L15 83.8C14.1 83.1 12.9 83 11.9 83.6L7 86.2C5.3 87.3 3.1 86.3 2.5 84.4L0.9 79.3C0.5 78.2 -0.5 77.4 -1.6 77.4H-6.8C-8.7 77.4 -10.2 75.9 -10.2 74V68.8C-10.2 67.7 -11 66.7 -12.1 66.3L-17.2 64.7C-19.1 64.1 -20.1 61.9 -19 60.2L-16.4 55.3C-15.8 54.3 -15.9 53.1 -16.6 52.2L-20 48C-21.1 46.6 -21.1 44.6 -20 43.2L-16.6 39C-15.9 38.1 -15.8 36.9 -16.4 35.9L-19 31C-20.1 29.3 -19.1 27.1 -17.2 26.5L-12.1 24.9C-11 24.5 -10.2 23.5 -10.2 22.4V17.2C-10.2 15.3 -8.7 13.8 -6.8 13.8H-1.6C-0.5 13.8 0.5 13 0.9 11.9L2.5 6.8C3.1 4.9 5.3 3.9 7 5L11.9 7.6C12.9 8.2 14.1 8.1 15 7.4L19.2 4C20.6 2.9 22.6 2.9 24 4Z" stroke="currentColor" stroke-width="2"/>
      <circle cx="24" cy="24" r="8" stroke="currentColor" stroke-width="2"/>
    </svg>`
  }
]

// 颜色配置
const colors = {
  normal: '#64748b',
  active: '#3b82f6'
}

// 输出目录
const outputDir = path.resolve(__dirname, '../src/assets')
const tempDir = path.resolve(__dirname, '../temp')

// 确保目录存在
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true })
}
if (!fs.existsSync(tempDir)) {
  fs.mkdirSync(tempDir, { recursive: true })
}

async function generateIcon(icon, color, isActive) {
  const svg = icon.svg.replace(/currentColor/g, color)
  const suffix = isActive ? '-active' : ''
  const svgFilename = `${icon.name}${suffix}.svg`
  const pngFilename = `${icon.name}${suffix}.png`
  const svgPath = path.join(tempDir, svgFilename)
  const pngPath = path.join(outputDir, pngFilename)

  try {
    // 写入临时 SVG 文件
    fs.writeFileSync(svgPath, svg)

    // 使用 svgexport 转换为 PNG
    await execAsync(`svgexport ${svgPath} ${pngPath} 96:96`)

    // 删除临时 SVG 文件
    fs.unlinkSync(svgPath)

    console.log(`Generated: ${pngFilename}`)
  } catch (error) {
    console.error(`Error generating ${pngFilename}:`, error)
  }
}

async function generateAllIcons() {
  console.log('Generating icons...')
  
  for (const icon of icons) {
    // 生成普通状态图标
    await generateIcon(icon, colors.normal, false)
    // 生成选中状态图标
    await generateIcon(icon, colors.active, true)
  }

  // 清理临时目录
  fs.rmdirSync(tempDir, { recursive: true })

  console.log('All icons generated successfully!')
}

// 添加到 package.json 的 scripts 中：
// "generate-icons": "node scripts/generateIcons.js"

generateAllIcons() 